package com.weng.cloud.commons.base;

import com.google.common.base.Stopwatch;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * @Author: weng
 * @Date: 2022/7/10
 * @Description: com.weng.cloud.commons.base
 * @see <a href="http://www.ibloger.net/article/3304.html">Google Guava 快速入门 —— 【基础】计时器 StopWatch 类</a>
 * @see <a href="https://www.cnblogs.com/kaituorensheng/p/7105639.html">spring StopWatch用法</a>
 */

public class TestUtil {

    public static Stopwatch create() {
        return Stopwatch.createUnstarted();
    }

    public static Stopwatch createAndStart() {
        return Stopwatch.createStarted();
    }

    public static Stopwatch start(Stopwatch stopWatch) {
        return stopWatch.start();
    }

    public static Stopwatch stop(Stopwatch stopWatch) {
        return stopWatch.stop();
    }

    public static Stopwatch reset(Stopwatch stopWatch) {
        return stopWatch.reset();
    }

    public static boolean isRunning(Stopwatch stopWatch) {
        return stopWatch.isRunning();
    }

    public static Duration watch(Stopwatch stopWatch) {
        return stopWatch.elapsed();
    }

    public static long watch(Stopwatch stopWatch, TimeUnit unit) {
        return stopWatch.elapsed(unit);
    }

    public static long watchSecond(Stopwatch stopWatch) {
        return watch(stopWatch, TimeUnit.SECONDS);
    }

    public static long watchMillis(Stopwatch stopWatch) {
        return watch(stopWatch, TimeUnit.MILLISECONDS);
    }

    public static long watchNanos(Stopwatch stopWatch) {
        return watch(stopWatch, TimeUnit.NANOSECONDS);
    }

    public static long stopAndWatch(Stopwatch stopWatch, TimeUnit unit) {
        return watch(stop(stopWatch), unit);
    }

    public static long stopAndWatchSecond(Stopwatch stopWatch) {
        return watchSecond(stop(stopWatch));
    }

    public static long stopAndWatchMillis(Stopwatch stopWatch) {
        return watchMillis(stop(stopWatch));
    }

    public static long stopAndWatchNanos(Stopwatch stopWatch) {
        return watchNanos(stop(stopWatch));
    }
}
